import { BaseCommand, TSelectRanage } from '../types/common.interface';
import { TableDatas } from '../cors/table-datas';
import { RenderTrigger } from '../cors/render-trigger';
import { NFill, NFont } from '../types/style.interface';
import { clone } from '../utils/deep-clone';

export class FontStyleCommand extends BaseCommand {
    protected pastFont: {row: number, col: number, font: NFont.IFont}[] = [];
    constructor(
        protected datas: TableDatas, 
        protected range: TSelectRanage, 
        protected renderTrigger: RenderTrigger,
        protected font: NFont.IFont
    ) {
        super();
    }
    public execute(): boolean {
        for(let i = this.range.row; i < this.range.row + this.range.rowspan; i++) {
            let rowInstance = this.datas.getRow(i);
            for(let j = this.range.col; j < this.range.col + this.range.colspan; j++) {
                let merge = this.datas.mergeCell.getMerge(i,j);
                if(merge != null && (merge.row != i || merge.col != j)) {
                    continue;
                }
                let cellInstance = rowInstance.getCell(j);
                let pastFont = clone(cellInstance.font);

                this.pastFont.push({
                    row: i,
                    col: j,
                    font: pastFont
                });
                cellInstance.font = Object.assign({}, cellInstance.font, this.font);
                rowInstance.setCell(j, cellInstance);
                this.datas.setRow(rowInstance, i);
            }
        }
        this.renderTrigger.triggerRenderAll();
        return true;
    }
    public redo() {
        for(let item of this.pastFont) {
            let rowInstance = this.datas.getRow(item.row);
            let cellInstance = rowInstance.getCell(item.col);
            cellInstance.font = item.font;
            rowInstance.setCell(item.col, cellInstance);
            this.datas.setRow(rowInstance, item.row);
        }
        return true;
    }
}